package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.JtDistributor;
import com.ruoyi.business.domain.JtDistributorRating;
import com.ruoyi.business.domain.JtSupplierRating;
import com.ruoyi.business.domain.vo.JtSupplierVo;
import com.ruoyi.business.service.IJtSupplierRatingService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.JtSupplierMapper;
import com.ruoyi.business.domain.JtSupplier;
import com.ruoyi.business.service.IJtSupplierService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商信息Service业务层处理
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Service
public class JtSupplierServiceImpl implements IJtSupplierService 
{
    @Autowired
    private JtSupplierMapper jtSupplierMapper;
    @Autowired
    private IJtSupplierRatingService jtSupplierRatingService;

    /**
     * 查询供应商信息
     * 
     * @param supplierId 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public JtSupplier selectJtSupplierBySupplierId(Long supplierId)
    {
        return jtSupplierMapper.selectJtSupplierBySupplierId(supplierId);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param jtSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<JtSupplier> selectJtSupplierList(JtSupplier jtSupplier)
    {
        return jtSupplierMapper.selectJtSupplierList(jtSupplier);
    }

    /**
     * 查询供应商信息列表
     *
     * @param jtSupplier 供应商信息
     * @return 供应商信息集合
     */
    @Override
    public List<JtSupplierVo> selectJtSupplierVoList(JtSupplier jtSupplier){
        return jtSupplierMapper.selectJtSupplierVoList(jtSupplier);
    }

    /**
     * 新增供应商信息
     * 
     * @param jtSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int insertJtSupplier(JtSupplier jtSupplier)
    {
        jtSupplier.setCreateBy(ShiroUtils.getSysUser().getUserName());
        jtSupplier.setCreateTime(DateUtils.getNowDate());
        return jtSupplierMapper.insertJtSupplier(jtSupplier);
    }

    /**
     * 修改供应商信息
     * 
     * @param jtSupplier 供应商信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJtSupplier(JtSupplier jtSupplier)
    {
        JtSupplier jtSupplierDb = selectJtSupplierBySupplierId(jtSupplier.getSupplierId());
        Object remarkObj = jtSupplier.getParams().get("remark");
        String remark = "";
        if(remarkObj != null) {
            remark = (String) remarkObj;
        }
        if(jtSupplierDb.getRating() != null){
            if(jtSupplier.getRating() == null){
                //修改为空
                addJtSupplierRating(jtSupplier.getSupplierId(), jtSupplier.getRating(), remark);
            } else {
                if(!jtSupplier.getRating().equals(jtSupplierDb.getRating())){
                    //修改值
                    addJtSupplierRating(jtSupplier.getSupplierId(), jtSupplier.getRating(), remark);
                }
            }
        } else {
            if(jtSupplier.getRating() != null){
                //第一次打分
                addJtSupplierRating(jtSupplier.getSupplierId(), jtSupplier.getRating(), remark);
            }
        }
        jtSupplier.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        jtSupplier.setUpdateTime(DateUtils.getNowDate());
        return jtSupplierMapper.updateJtSupplier(jtSupplier);
    }

    private int addJtSupplierRating(Long supplierId, Integer rating, String remark){
        JtSupplierRating entity = new JtSupplierRating();
        entity.setSupplierId(supplierId);
        entity.setRater(ShiroUtils.getSysUser().getUserName());
        entity.setRating(rating);
        entity.setRemark(remark);
        return jtSupplierRatingService.insertJtSupplierRating(entity);
    }

    /**
     * 批量删除供应商信息
     * 
     * @param supplierIds 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteJtSupplierBySupplierIds(String supplierIds)
    {
        return jtSupplierMapper.deleteJtSupplierBySupplierIds(Convert.toStrArray(supplierIds));
    }

    /**
     * 删除供应商信息信息
     * 
     * @param supplierId 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteJtSupplierBySupplierId(Long supplierId)
    {
        return jtSupplierMapper.deleteJtSupplierBySupplierId(supplierId);
    }
}
